import { getFolder, getParentFoldersApi, getSubFolderTree } from "@/api/folder";
import { labelTypePrivate, labelTypeTeam } from "@/constant";

/** 查找文件夹的路径 */
export const getPathListAsync = async ({ ASSET_LABEL_SEQ }) => {
    if(!ASSET_LABEL_SEQ) throw new Error("ASSET_LABEL_SEQ 不存在");

    const { _success, _data } = await getParentFoldersApi(ASSET_LABEL_SEQ);
    if (!_success) {
        return [];
    }
    const path = [];
    for (let i = 0; i < _data.length; i++) {
        const resp = await getFolder(_data[i]);
        if (!resp._success) {
            return [];
        }
        resp._data.ASSET_LABEL_SEQ = parseInt(resp._data.ASSET_LABEL_SEQ);
        path.push(resp._data);
    }
    return path;
}
/** 获取文件夹的子文件夹 */
export const getSubFoldersAsync = async (folderType, parentFolder) => {
    let parentSeq = "-1"; //顶层目录
    if (parentFolder && parentFolder.ASSET_LABEL_SEQ) {
        parentSeq = parentFolder.ASSET_LABEL_SEQ;
    }
    let params = {
        folderId: parentSeq,
        folderType
    };

    const { _success, _data } = await getSubFolderTree(params);
    if (!_success) {
        throw "加载子目录树失败";
    }
    const childrenNodes = _data.map(node => ({
        ...node,
        isLoading: node.hasNext, //如果有下一级目录，则还需要加载下一级目录
        children: undefined
        // loaded: true,
        // isLeaf: !node.hasNext
    }));
    return childrenNodes;
};

export const getFolderTypeStore = folderType => {
    if (folderType === labelTypeTeam) {
        return "team-workspace";
    } else if (folderType === labelTypePrivate) {
        return "personal-workspace";
    }
    throw "无效的 folderType";
};
